using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Xfinity.Engine;

namespace Xfinity.AI.Entities
{
    /// <summary>
    /// An entity that has a physical presence in the game world.
    /// </summary>
    public class PhysicalEntity : Entity, IDrawable, IPhysicalEntity
    {
        /// <summary>
        /// Constructs a new physical object.
        /// </summary>
        /// <param name="name">The name of the object.</param>
        /// <param name="game">The Game.</param>
        public PhysicalEntity(string name, Game game)
            : base(name, game)
        {
            Visible = false;
        }
        public PhysicalEntity()
            : this("", null)
        {

        }
        /// <summary>
        /// Constructs a new physical entity.
        /// </summary>
        /// <param name="name">The name of the object.</param>
        /// <param name="fileName">The filename of the model.</param>
        /// <param name="game">The Game.</param>
        public PhysicalEntity(string name, string fileName, Game game)
            : base(name, game)
        {
            modelFileName = fileName;
            Visible = false;
        }
        private float health;

        private Model model;
        /// <summary>
        /// The model of the entity.
        /// </summary>
        /// <value>The model.</value>
        [XmlIgnore]
        [ContentSerializerIgnore]
        public Model Model
        {
            get { return model; }
            set
            {
                if (value != null)
                {
                    model = value; modelFileName = model.Root.Name;
                }
            }
        }
        private string modelFileName;

        /// <summary>
        /// The filename of the model.
        /// </summary>
        /// <value>The name of the model file.</value>
        //        [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
        public string ModelFileName
        {
            get
            {
                if (String.IsNullOrEmpty(modelFileName)) { return modelFileName; }
                if (model != null) { return model.Root.Name; }
                return "";
            }
            set { modelFileName = value; }
        }

        /// <summary>
        /// The health of the object.
        /// </summary>
        /// <value>The health.</value>
        public float Health
        {
            get { return health; }
            set
            {
                health = value;
                if (health <= 0 && Death != null)
                {
                    Death(this, EventArgs.Empty);
                }
            }
        }

        private Vector3 position;

        /// <summary>
        /// The origin of the entity. Can be any valid Vector3.
        /// </summary>
        /// <value>The origin of the entity.</value>
        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }
        /// <summary>
        /// Initialises the entity.
        /// </summary>
        public override void Initialize()
        {
            Death += new EventHandler(Die);
            if (model != null)
            {
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (Effect e in mesh.Effects)
                    {
                        if (e is BasicEffect)
                        {
                            BasicEffect be = e as BasicEffect;
                            be.PreferPerPixelLighting = true;
                            be.EnableDefaultLighting();
                        }
                    }
                }
            }
            base.Initialize();
        }

        private float mass;
        /// <summary>
        /// The mass of the entity.
        /// </summary>
        /// <value>The mass.</value>
        public float Mass
        {
            [DebuggerStepThrough]
            get { return mass; }
            [DebuggerStepThrough]
            set { mass = value; }
        }

        /// <summary>
        /// Called when the entity's health drops below 0.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private void Die(object sender, EventArgs e)
        {
            Enabled = false;
        }
        /// <summary>
        /// Raised when the entity's health equals or drops below 0.
        /// </summary>
        public event EventHandler Death;

        #region IDrawable Members
        /// <summary>
        /// Draws the entity.
        /// </summary>
        /// <param name="gameTime">The elapsed time.</param>
        public override  void Draw(GameTime gameTime)
        {
            GraphicsHelper.DrawClippedModel(model);
        }

        

        #endregion
    }
}